using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using User.Models;
using User.Service;
using UserLogicCore.Logic;
using UserSorce.Entity.Models;

namespace User.Controllers
{
    public class UserController : Controller
    {
        //用户方法
        private UserService _ctx = new UserService();
        private UserServiceModels _userInformation= new UserServiceModels();

        [HttpGet]
        public IActionResult Index()
        {
            return View("Login");
           //  return View();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(string userName, string password)
        {
            int userId = _ctx.FindUser(userName, password);
            TypeController.UserId = userId;
            if (userId!= 0)
            {
               HttpContext.Session.SetString("user", "userId");

                List<Models.TypesModels> viewTypeList = new List<Models.TypesModels>();
                Users userData = _ctx.GetData(userId);
                var user = _userInformation.TurnOffUserData(userData);
            
                ViewData["Name"] = user.NickName;
                 return View("Type", user);
            }
            else
            {
                ViewData["Message"] = "你的用户名或密码错误";
                return View("Login");
            }
        }
        [HttpGet]
        public IActionResult Register()
        {
            return View("Register");
        }
        /// <summary>
        ///注册用户 
        /// </summary>
        /// <param name="newUserName"></param>
        /// <param name="password"></param>
        /// <param name="rePassword"></param>
        /// <param name="nickName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateData(string newUserName, string password,string rePassword, string nickName)
        {
            if(password==null||newUserName ==null || rePassword == null)
            {
                ViewData["Message"] = "输入不能为空";
                return View("Register");
            }
           else if (password.Equals(rePassword))
            {
                if(_ctx.EnrollUser(newUserName, password, nickName))
                    return View("Login");
                else
                {
                    ViewData["Message"] = "你输入用户名已存在";
                    return View("Register");
                }
            }
            else
            {
                ViewData["Message"] = "你输入的两次密码不一致";
                return View("Register");
            }
               

        }
 
        /// <summary>
        ///从注册界面返回到登录界面 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Goback()
        {
            return View("Login");
        }
    }
}